
package com.hyst.seviceImpl;

import java.util.*;

import com.hyst.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyst.domain.Teacher;
import com.hyst.domain.TeacherStudent;
import com.hyst.repository.TeacherMapper;
import com.hyst.service.TeacherService;
import com.hyst.utils.UuidTool;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:
 * @Date：2018年08月21日 
 * @author： dy 
 * @version ：1.0 1.[2018年08月21日]
 */
@Service
public class TeacherServiceImpl implements TeacherService {

	@Autowired
	private TeacherMapper mapper;

	@Override
	public Page<Teacher> list(final Teacher teacher) {
		final Page<Teacher> page = PageHelper.startPage(teacher.getPageNum(), teacher.getPageSize());
//		teacher.setIsDelete(null);
//		List<Teacher> list = mapper.select(teacher);
//		Collections.sort(list,(s1, s2) -> Integer.compare(s1.getName(), s2.getName()));
		List<Teacher> list = mapper.getAllTeachers();


		for(Teacher teacher1: page.getResult()){
			Integer integer = mapper.gettryLister(teacher1.getUniqueid());
            Integer learnCount = mapper.getLearnCount(teacher1.getUniqueid());
            teacher1.setTryListernCount(integer);
            teacher1.setLeanCount(learnCount);
		}
		return page;
	}

	@Override
	public Teacher insert(final Teacher teacher) {
		try {
			teacher.setUniqueid(UuidTool.randomUUID());
			teacher.setCreatetime(DateUtils.formatDate(new Date(),"yyyy-MM-dd"));
			teacher.setPassword("MTIzNDU2");
			mapper.insertSelective(teacher);
			return teacher;
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Teacher getByPriKey(final String uniqueid) {
//		final Teacher teacher = mapper.selectByPrimaryKey(uniqueid);
		final Teacher teacher = mapper.selectById(uniqueid);
		return teacher;
	}

	@Override
	public Teacher updateSelective(final Teacher teacher) {
		mapper.updateByPrimaryKeySelective(teacher);
		return teacher;
	}

	@Override
	public Teacher update(final Teacher teacher) {
		mapper.updateByPrimaryKey(teacher);
		return teacher;
	}


	@Transactional
	@Override
	public Teacher delete(final String uniqueid) {
		Teacher teacher=mapper.selectByPrimaryKey(uniqueid);
		teacher.setIsDelete(1);
		teacher.setAccount("delete");
		mapper.updateByPrimaryKey(teacher);

		//删除课程记录
		mapper.deletePeriod(uniqueid);

		//更新试听管理信息
//		mapper.updatePeriod(uniqueid);

		//更新学生免费试听信息
//		mapper.updateFreePeriod(uniqueid);
		return teacher;
	}

	@Override
	public Teacher state(Teacher teacher) {
		if(teacher.getSetTop()!=null&&teacher.getSetTop()==1){
			teacher.setSortNum(mapper.getMaxSort()+3);
		}
		mapper.updateByPrimaryKeySelective(teacher);
		return teacher;
	}

	@Override
	public List<Map> getSelect2Data() {
		return mapper.getSelect2Data();
	}

	@Override
	public List<Map> getSelect3Data(String key) {
		return mapper.getSelect3Data(key);
	}

	@Override
	public Page<TeacherStudent> getMyStudents(String teacherId) {
		Page<TeacherStudent> page = PageHelper.startPage(1, 2000);
		mapper.getMyStudents(teacherId);
		return page;
	}

}
